<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>组织架构</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true" :model="searchUserForm" :rules="searchUserFormRules" ref="searchUserFormRef" size="small">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="searchUserForm.username" @keyup.enter.native="searchUser" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="searchUserForm.mail" @keyup.enter.native="searchUser" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="depart">
          <el-input v-model="searchUserForm.depart" @keyup.enter.native="searchUser" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="禁用状态" prop="disabled">
          <el-select v-model="searchUserForm.disabled">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchUser">查询</el-button>
          <el-button type="warning" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="userListData" border stripe style="width: 100%" size="mini">
        <el-table-column prop="username" label="姓名" width="115" align="center"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="115" align="center"></el-table-column>
        <el-table-column prop="depart" label="部门" width="115" align="center"></el-table-column>
        <el-table-column prop="disabled" label="状态" width="115" align="center">
          <template slot-scope="scoped" v-if="scoped.row.username !== 'admin'">
            <el-tooltip :content="'状态: ' + (scoped.row.disabled == 1 ? '禁用' : '启用')" placement="top">
              <el-switch v-model="scoped.row.disabled" active-color="#ff4949" inactive-color="#13ce66" active-value="1" inactive-value="0" @change="changeSwitch(scoped.row)"> </el-switch>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="editUser(scope.row)" type="primary" size="mini" class="tableRowButton">编辑</el-button>
            <el-popover placement="top" width="80" :ref="`popover-${scope.$index}`" v-if="scope.row.username !== 'admin'">
              <p>确定要删除用户{{ scope.row.username }}吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="cancelAction(scope)">取消</el-button>
                <el-button type="primary" @click="delUser(scope)" size="mini">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchUserForm.pagenum"
        :page-sizes="[10, 20, 50]"
        :page-size="searchUserForm.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="编辑信息" :visible.sync="dialogUserEditFormVisible">
      <el-form :model="userEditForm" size="small" label-width="50px" label-position="right">
        <el-form-item label="用户">
          <el-input v-model="userEditForm.username" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="email">
          <el-input v-model="userEditForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="userEditForm.depart" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editSaveUser" size="small">保 存</el-button>
        <el-button @click="dialogUserEditFormVisible = false" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchUserForm: {
        username: '',
        mail: '',
        depart: '',
        disabled: '',
        pagenum: 1,
        pagesize: 10
      },
      searchUserFormRules: {
        username: [{ max: 255, message: '长度不大于255个字符', trigger: 'blur' }],
        mail: [{ max: 17, message: '长度不大于17个字符', trigger: 'blur' }],
        depart: [{ max: 8, message: '长度不大于8个字符', trigger: 'blur' }],
        disabled: [{}]
      },
      userListData: [],
      total: 0,
      userEditForm: {},
      dialogUserEditFormVisible: false
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 搜索
    searchUser() {
      this.$refs.searchUserFormRef.validate((valid) => {
        if (valid === false) return false
        this.getUserList()
      })
    },
    // 重置
    resetForm() {
      this.$refs.searchUserFormRef.resetFields()
      this.getUserList()
    },
    // 获取用户方法
    async getUserList() {
      const { data: res } = await this.$http.get('api/org/userlist', { params: this.searchUserForm })
      this.userListData = res.data
      console.log(this.userListData)
      this.total = res.total
    },
    // 调整每页显示行数事件
    handleSizeChange(size) {
      this.searchIpForm.pagesize = size
      this.getUserList()
    },
    // 翻页事件
    handleCurrentChange(num) {
      this.searchIpForm.pagenum = num
      this.getUserList()
    },
    // 编辑用户
    editUser(row) {
      this.dialogUserEditFormVisible = true
      this.userEditForm = JSON.parse(JSON.stringify(row))
      this.$delete(this.userEditForm, 'disabled')
      console.log(this.userEditForm)
    },
    async editSaveUser() {
      console.log(this.userEditForm)
      const { data: res } = await this.$http.put(`api/org/user/${this.userEditForm.id}`, this.userEditForm)
      if (res.code !== 0) return this.$message.error('更新失败！')
      this.$message.success('保存成功！')
      this.dialogUserEditFormVisible = false
      this.getUserList()
    },
    // 删除
    async delUser(scope) {
      console.log(scope.row)
      const { data: res } = await this.$http.delete(`/api/org/user/${scope.row.id}`)
      if (res.code !== 0) return this.$message.error('删除失败！')
      this.$message.success('删除成功！')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
      this.getUserList()
    },
    // 取消操作
    cancelAction(scope) {
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    },
    // 点击禁用开关
    async changeSwitch(row) {
      const { data: res } = await this.$http.put(`api/org/user/${row.id}`, { disabled: row.disabled })
      if (res.code !== 0) {
        this.$message.error('更新失败！')
        row.disabled = !row.disabled
      } else {
        this.$message.success('更新成功！')
        this.getUserList()
      }
    },
    // 获取网段
    async getSegment() {
      const { data: res } = await this.$http.get('api/ip/segment')
      this.segmentListData = res.data
      console.log(res.data)
    }
  }
}
</script>

<style lang="less" scoped>
.tableRowButton {
  margin-right: 5px;
}
</style>
